package com.example.administrator.x3picturetransliteration.Home.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.x3picturetransliteration.Base.BaseActivity;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Activity.AdvertisementPresenter;
import com.example.administrator.x3picturetransliteration.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2019/8/14.
 */

public class GuideActivity extends BaseActivity{

    @BindView(R.id.MeViewPage)ViewPager MeViewPage;

    private AdvertisementPresenter mAdvertisementPresenter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_guide;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MeViewPage.setAdapter(new ViewPageAdapter(setView()));

        mAdvertisementPresenter = new AdvertisementPresenter(this,mCompositeSubscriptions);

    }

    private class ViewPageAdapter extends PagerAdapter{

        private List<View> views;

        public ViewPageAdapter(List<View> views){

            this.views = views;
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {

            container.addView(views.get(position));
            return views.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(views.get(position));
        }
    }

    private List<View> setView(){

        List<View> views = new ArrayList<>();

        for (int i=0;i<3;i++){

            View mview = LayoutInflater.from(this).inflate(R.layout.home_view_guide,null);

            ImageView img = mview.findViewById(R.id.img);

            final LinearLayout IconView = mview.findViewById(R.id.IconView);

            View[] IconViews = new View[3];
            IconViews[0] = mview.findViewById(R.id.IconView1);
            IconViews[1] = mview.findViewById(R.id.IconView2);
            IconViews[2] = mview.findViewById(R.id.IconView3);

            ImageView[] IconImgs = new ImageView[3];
            IconImgs[0] = mview.findViewById(R.id.IconImg1);
            IconImgs[1] = mview.findViewById(R.id.IconImg2);
            IconImgs[2] = mview.findViewById(R.id.IconImg3);

            TextView ContentText = mview.findViewById(R.id.ContentText);

            ImageView DianView = mview.findViewById(R.id.DianImg);
            TextView GoText = mview.findViewById(R.id.GoText);

            switch (i){

                case 0:

                    img.setImageResource(R.drawable.home_pic1);
                    IconImgs[0].setImageResource(R.mipmap.home_guide1);
                    IconImgs[1].setImageResource(R.mipmap.home_guide2);
                    IconImgs[2].setImageResource(R.mipmap.home_guide3);
                    ContentText.setText("智能识别文字    印刷字体识别" + "\n\n" + "手写文字识别");
                    DianView.setImageResource(R.mipmap.home_guide0);

                    break;

                case 1:

                    img.setImageResource(R.drawable.home_pic2);
                    IconViews[0].setVisibility(View.GONE);
                    IconViews[1].setVisibility(View.GONE);
                    IconImgs[0].setImageResource(R.mipmap.home_guide4);
                    ContentText.setText("高精度识别各行文档和表单表格" + "\n\n" + "word表格  excel表格");
                    DianView.setImageResource(R.mipmap.home_guide02);



                    IconView.post(new Runnable() {
                        @Override
                        public void run() {

                            RelativeLayout.LayoutParams linearParams =(RelativeLayout.LayoutParams) IconView.getLayoutParams();
                            linearParams.width = IconView.getWidth()/3;
                            IconView.setLayoutParams(linearParams);
                        }
                    });

                    break;

                case 2:

                    img.setImageResource(R.drawable.home_pic3);
                    IconImgs[0].setImageResource(R.mipmap.home_guide5);
                    IconImgs[1].setImageResource(R.mipmap.home_guide6);
                    IconImgs[2].setImageResource(R.mipmap.home_guide7);
                    ContentText.setText("拍照翻译    支持多国语言");
                    DianView.setVisibility(View.GONE);
                    GoText.setVisibility(View.VISIBLE);

                    GoText.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            mAdvertisementPresenter.ad(new BasePresenter.Callback() {
                                @Override
                                public void getData(Object data) {

                                    if (data==null){

                                        startActivity(new Intent(GuideActivity.this,SignInActivity.class));


                                    }else {

                                        AdvertisementActivity.StartAdvertisementActivity(GuideActivity.this);
                                    }

                                    GuideActivity.this.finish();

                                }
                            });

                        }
                    });

                    break;

            }

            views.add(mview);

        }

        return views;

    }
}
